<?php

namespace app\admin\model\xmwms;

use think\Model;

class OutboundOrderDetail extends Model
{
    // 表名
    protected $name = 'wms_outbound_order_detail';

    /**
     * @var string 自动写入时间戳字段
     */
    protected $autoWriteTimestamp = true;

    protected $append = [
        'warehouse_shelf_id_text',
    ];

    // 字段类型转换
    protected $type = [
        'deletetime' => 'timestamp:Y-m-d H:i:s',
    ];

    public function getWarehouseShelfIdTextAttr($value, $row): string
    {
        if (isset($row['warehouse_shelf_id']) && $row['warehouse_shelf_id'] > 0){
            $model = new Warehouse();
            $data = $model->where('id',$row['warehouse_shelf_id'])->find();
            if ($data['warehouse_type'] == 'warehouse_shelf'){
                $warehouse_area = $model->where('id',$data['pid'])->find();
                $warehouse = $model->where('id',$warehouse_area['pid'])->value('name');
                return $warehouse . '/' . $warehouse_area['name'] . '/' . $data['name'];
            }elseif ($data['warehouse_type'] == 'warehouse_area'){
                $warehouse = $model->where('id',$data['pid'])->value('name');
                return $warehouse . '/' . $data['name'];
            }else{
                return $data['name'];
            }
        }else {
            return '';
        }
    }

    public function outboundorder()
    {
        return $this->belongsTo(OutboundOrder::class, 'outbound_no', 'outbound_no');
    }

    public function product(){
        return $this->belongsTo(Product::class,'product_id','id');
    }

    public function wmsCustomer()
    {
        return $this->belongsTo(\app\admin\model\xmwms\Customer::class, 'customer_id', 'id');
    }

}